from configparser import ConfigParser

config = ConfigParser()
config.read('../config.ini', encoding='utf-8')


class SysConfig:
    def __init__(self, _user, _password, _ip, _host):
        self.user = _user
        self.password = _password
        self.ip = _ip
        self.host = _host

    @classmethod
    def get_base_config_list(cls):
        user = config['base']['user']
        password = config['base']['password']
        base_config_list = []
        ip_hosts = config['base']['ip_hosts'].split(',')

        for item in ip_hosts:
            item = item.split(":")
            base_config_list.append(SysConfig(_user=user, _password=password, _ip=item[0], _host=item[1]))
        return base_config_list

    def __str__(self):
        return "user:{},password:{},ip:{},host:{}".format(self.user, self.password, self.ip, self.host)


class SparkConf:
    def __init__(self, _host, _is_master, _is_slave):
        self.host = _host
        self.is_master = _is_master
        self.is_slave = _is_slave

    @classmethod
    def get_spark_conf_list(cls):
        master = config['spark']['master']
        slaves = config['spark']['slave'].split(',')
        spark_hosts = set(slaves)
        spark_hosts.add(master)
        spark_conf_list = []
        for host in spark_hosts:
            is_master = host == master
            is_slave = host in slaves
            spark_conf_list.append(SparkConf(host, is_master, is_slave))
        return spark_conf_list

    def __str__(self):
        return "spark:[host:{},is_master:{},is_slave:{}]".format(self.host, self.is_master, self.is_slave)


if __name__ == '__main__':
    sparklist = SparkConf.get_spark_conf_list()
    for i in sparklist:
        print(i)
